const app=getApp();
Page({
  data: {
    collectarr:[],
    arrayList:[],
    arr:''
  },
     //判断有没有收藏
     iscollect(e){
      wx.request({
        url:`${app.globalData.url}/user/collect`,
        dataType:'json',
        method:'GET',
        header:{
          "content-type":'application/json',
          authorization: wx.getStorageSync('token')
        },
        success:res=>{
            console.log(res.data);
            this.setData({
              collectarr:res.data
            })
            var length=this.data.collectarr.length;
            console.log(length);
            if(length==0){
              this.collected(e)
            }
            else if(length!=0){
              let flag='false';
              for(var i=0;i<length;i++){
                //依照名字对比数据库中是否收藏了
                var name=this.data.collectarr[i].name;
                if(name==this.data.arr.wear_name){
                  //如果收藏了更改标志位
                  flag='true';
                  wx.showToast({
                    title: '您已收藏过该内容',
                    duration: 1000,
                    mask: false,
                    icon:'none'
                  })
                }
              }
              if(flag=='false'){
                  this.collected(e)
              }
            }
            
        },
        fail(){
          console.log("no");
        }
      })
    },
    //如果没有收藏的话进行收藏
    collected(e){
      var id=e.currentTarget.id;
      var name=this.data.arr.wear_name;
      // var timestamp = Date.parse(new Date());  
      // timestamp = timestamp / 1000; 
      let tm = new Date();
      var collection_time = `${tm.getFullYear()}-${tm.getMonth()+1}-${tm.getDate()} `
          + `${tm.getHours()}:${tm.getMinutes()}:${tm.getSeconds()}`;
    
      var collection_text='暂无';
      var collection_image=this.data.arr.wear_image;
      var anothertext=this.data.arr.wear_text;
      var type='true';
      wx.request({
        url:`${app.globalData.url}/user/collected`,
        header: {
          "Content-Type": "application/json",
          authorization: wx.getStorageSync('token')
        },
        method: "POST",
         data: {collection_time,collection_text,collection_image,name,anothertext,type},
          success:(res)=>{
          },
          fail(){
            wx.showToast({
              title:'收藏失败',
              icon:'none'
            })
          }
        })
    },
    //点击按钮的时候首先查看是不是登录了
    collect(e){
      var islogin=wx.getStorageSync('isShow') ;
      if(islogin){
        console.log("collect")
        this.iscollect(e);
      }else{
        wx.showModal({
          title: '提示',
          content: '用户未登录！',
          success: function (res) {
            if (res.confirm) {//这里是点击了确定以后
              wx.switchTab({
                url: '/pages/getuserinfo/getuserinfo',
              })
            } else {//这里是点击了取消以后
              console.log('用户点击取消')
            }
          }
        })
    
      }
   
    },
  wearWhat(){
    var id=Math.floor(Math.random()*3);
    var arr=this.data.arrayList[id];
    this.setData({arr:arr})
    console.log(arr)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   //数据加载
   wx.showLoading({
    title: '数据加载中'
  });
    wx.request({
      url:`${app.globalData.url}/wearbook`,
      dataType:'json',
      method:'GET',
      header:{
        "content-type":'application/json'
      },
      success:res=>{
          console.log(res.data);
          this.setData({
            arrayList:res.data
            // 将获取到的数据传给初始化的变量，在后端渲染
          })
          this.wearWhat();
          wx.hideLoading({
          })
      },
      fail(){
        console.log("no");
      }
    })
   
  }
})